import Vue from 'vue'
import VueRouter from 'vue-router';

Vue.use(VueRouter);


const loginPage = r => require.ensure([], () => r(require('../view/login/LoginPage')), 'loginPage');
const userInfo = r => require.ensure([], () => r(require('../view/exam/userInfo')), 'userInfo');
const userInfoImg = r => require.ensure([], () => r(require('../view/exam/userInfoImg')), 'userInfoImg');
const enroll = r => require.ensure([], () => r(require('../view/exam/enroll')), 'enroll');
const enrollImg = r => require.ensure([], () => r(require('../view/exam/enrollImg')), 'enrollImg');
const signIn = r => require.ensure([], () => r(require('../view/exam/signIn')), 'signIn');
const rpExam = r => require.ensure([], () => r(require('../view/exam/rpExam')), 'rpExam');

const routes = [
  {path: '*', redirect: '/loginPage'},
  {name: 'loginPage', component: loginPage},
  {name: 'userInfo', component: userInfo},
  {name: 'userInfoImg', component: userInfoImg},
  {name: 'enroll', component: enroll},
  {name: 'enrollImg', component: enrollImg},
  {name: 'signIn', component: signIn},
  {name: 'rpExam', component: rpExam},
];

// 懒得写path，给他们自动生成一个，和name同名
routes.forEach(route => {
  route.path = route.path || '/' + (route.name || '');
});

const router = new VueRouter({routes});

// 生命周期之前
router.beforeEach((to, from, next) => {
  // 保存个路由来路，以备不时之需
  localStorage.setItem('fromPage', from.name);
  next();
});

//生命周期之后
router.afterEach((to, from) => {
  // 滑动到顶吧
  window.scrollTo(0, 0);
});

export default router;
